Ext.define('Isms.controller.FileController', {
	extend : 'Ext.app.Controller',
	stores : ['FileStore'],
	models : [],
	views : [
			// 'client.ShowPanel',
			'data-doc.UploadFile', 'data-doc.FileList', 'data-doc.EditFile'
	// 'Ext.window.MessageBox',
	// 'Ext.tip.*'
	],
	init : function() {
		var vFillAdd = null;
		this.control({

			'file-list actioncolumn' : {
				click : this.handleActionColumn
			},
			'file-list>grid' : {
				render : function() {
					var grid = Ext.getCmp('filelist');
					grid.store.loadPage(1);
				}
			},
			'file-list button[action = file_edit_action]' : {
				click : this.handleEdit
			},
			'file-list button[action = file-add-action]' : {
				click : function(btn) {
					if (!vFillAdd) {
						vFillAdd = Ext.create('widget.window', {
									title : '发布资料文档',
									closable : true,
									closeAction : 'hide',
									width : 600,
									minWidth : 350,
									height : 350,
									items : [{
												xtype : 'upload-file'
											}]
								});
					}
					vFillAdd.show();
				}
			},
			'file-list button[action = file-del-action]' : {
				click : function(btn) {
					Ext.MessageBox.confirm('警告！！', '确定删除？？？？', function(btn) {
						if (btn == "yes") {
							var mFile_list = Ext.getCmp('filelist');
							var mDatas = mFile_list.getSelectionModel()
									.getSelection();
							var params = {};
							params.id = new Array();
							for (var i = 0; i < mDatas.length; i++) {
								params.id[i] = mDatas[i].get('id');
							}
							Ext.Ajax.request({
										url : '../file/delFiles.do',
										params : params,
										method : 'get',// 请求方式
										success : function(respsonse) {
											Ext.Msg.alert('提示', '删除成功',
													function() {
														// 重新加载数据
														Ext.getCmp('filelist').store
																.loadPage(1);
													});

										},
										failure : function(respsonse) {
											showMessage(response);
										}
									});

						}
					});
				}
			},
			'upload-file button[action = file_upload]' : {
				click : function(obj) {
					if (obj.text == '上传') {
						var form = obj.up('form').getForm();
						if (form.isValid()) {
							form.submit({
										url : '../file/addFile.do',
										params : form.getValues(true),
										method : 'POST',
										waitTitle : '请稍后',
										waitMsg : '正在上传文档文件 ...',
										success : function(form, action) {
											Ext.MessageBox.alert("提示", "操作成功！",
													function() {
														// 重新加载数据
														Ext.getCmp('filelist').store
																.loadPage(1);
														// 关闭窗口
														vFillAdd.close();
													});
										},
										failure : function(form, action) {
											Ext.MessageBox.alert("错误！",
													"操作失败<br>服务报告了一项错误，或连接失败");
										}
									});
						}
					};
				}
			},
			'edit-file button[action = file_edit]' : {
				click : this.editFile
			}
		});
	},
	editFile : function(btn) {
		var form = btn.up('form').getForm();
		if (form.isValid()) {
			form.submit({
						url : '../file/editFile.do',
						params : form.getValues(true),
						method : 'POST',
						waitTitle : '请稍后',
						waitMsg : '正在上传文档文件 ...',
						success : function(form, action) {
							Ext.MessageBox.alert("提示", "操作成功！", function() {
										// 重新加载数据
										Ext.getCmp('filelist').store
												.loadPage(1);
										// 关闭窗口
										vFileEdit.close();
									});
						},
						failure : function(form, action) {
							Ext.MessageBox.alert("错误！",
									"操作失败<br>服务报告了一项错误，或连接失败");
						}
					});
		}
	},
	handleEdit : function(btn) {
		// var vFileEdit = null;
		var rec = Ext.getCmp('filelist').getSelectionModel().getLastSelected();
		console.log(rec);
		var obj = Ext.getCmp('edit-file');
		if (!vFileEdit) {
			vFileEdit = Ext.create('widget.window', {
						title : '发布资料文档',
						closable : true,
						closeAction : 'hide',
						width : 600,
						minWidth : 350,
						height : 350,
						items : [{
									xtype : 'edit-file'
								}]
					});
		}
		Ext.getCmp('file_id').setValue(rec.get('id'));
		Ext.getCmp('file_title').setValue(rec.get('title'));
		Ext.getCmp('file_keyword').setValue(rec.get('keyword'));
		Ext.getCmp('file_describe').setValue(rec.get('describe'));
		vFileEdit.show();
	},
	handleActionColumn : function(view, cell, row, col, e) {
		var m = e.getTarget().className.match(/\bicon-(\w+)\b/);
		var rec = Ext.getCmp('filelist').getStore().getAt(row);// 选中行的数据
		console.log(m, view, cell, row, col, e);
		if (m) {
			switch (m[1]) {
				case 'download' :// 下载
					window.location.href = "../uploadFiles/" + rec.get('path');
					break;
			}
		}
	}
});

var vFileEdit = null;
function showMessage(response) {
	var obj = Ext.util.JSON.decode(response.responseText);
	Ext.Msg.alert('提示', obj.message);
};